<%@ Page language="vb" CodeFile="activateInvoicePriceListModification.aspx.vb" Inherits="activateInvoicePriceListModification_aspx_vb" %>

<%
    rowNumber = 1
    invoicePriceListModificationID = Request.QueryString("invoicePriceListModificationID")
    If Len(invoicePriceListModificationID) > 0 Then 
        invoicePriceListModificationID = CLng(invoicePriceListModificationID)
    End IF
    If (Request.QueryString("do")) = Nothing Then
        subDo = Request.QueryString("subDo")
        If Len(invoicePriceListModificationID) > 0 Then
            dbOpen()
            RecSet = Conn.Execute("Select iplm.paymentTerm, p.partnerName From (tblInvoicePriceListModification iplm INNER JOIN tblPartner p ON p.partnerID = iplm.partnerID) Where invoicePriceListModificationID = " & invoicePriceListModificationID & "")
            If RecSet.EOF Then
                Response.Redirect("activateInvoicePriceListModification.aspx")
            Else
                paymentTerm = RecSet.Fields("paymentTerm").Value
                partnerName = RecSet.Fields("partnerName").Value
            End If
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End If
        Over()
        strJS = ""
        strJS = strJS + "[""invoiceDateYear"", ""<1""],"
        strJS = strJS + "[""invoiceDateMonth"", ""<1""],"
        strJS = strJS + "[""invoiceDateDay"", ""<1""]"
        checkFields()
        realTime = Now()
        year = Left(realTime, 4)
        month = Mid(realTime, 6, 2)
        day = Mid(realTime, 9, 2)
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>ACTIVATE AN PRICE MODIFICATION INVOICE<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='activateInvoicePriceListModification.aspx?do=save&invoicePriceListModificationID=" & invoicePriceListModificationID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Invoice ID:</td><td align='right'><input type='text' name='invoicePriceListModificationID' id='invoicePriceListModificationID' value='" & invoicePriceListModificationID & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Partner:</td><td align='right'><input type='text' name='partnerName' id='partnerName' value='" & partnerName & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Payment term (days):</td><td align='right'><input type='text' name='paymentTerm' id='paymentTerm' value='" & paymentTerm & "' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Invoice date:&nbsp; </td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='invoiceDateYear' id='invoiceDateYear'>")
        Response.Write("<option value=''>&nbsp; Year ")
        stopYear = CDbl(year) + 3
        yearCounter = year
        For yearLoop = year To stopYear
            Response.Write("<option value='" & yearCounter & "'")
            If CStr(yearCounter) = year Then
                Response.Write("selected='selected'")
            End If
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateMonth' id='invoiceDateMonth'>")
        Response.Write("<option value=''>&nbsp; Month ")
        yearCounter = 1
        For yearLoop = 1 To 12
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(month) Then
                Response.Write("selected='selected'")
            End If
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateDay' id='invoiceDateDay'>")
        Response.Write("<option value=''>&nbsp; Day&nbsp; ")
        yearCounter = 1
        For yearLoop = 1 To 31
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(day) Then
                Response.Write("selected='selected'")
            End If
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td>")
        Response.Write("<div id='error_invoiceDateYear' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateMonth' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateDay' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("</td>")
        Response.Write("</tr>")
        If Len(invoicePriceListModificationID) > 0 Then
            Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Save'></td></tr>")
        End If
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='4'>QUEUED INVOICES<hr class='hrHeadline'></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select iplm.invoicePriceListModificationID, p.partnerName, c.country From ((tblInvoicePriceListModification iplm INNER JOIN tblPartner p ON p.partnerID = iplm.partnerID) INNER JOIN tblCountry c ON c.countryID = iplm.countryID) Where iplm.invoiceAmount = 0 AND iplm.invoicePrinted = 0")
        If RecSet.EOF Then
            Response.Write("<tr><td><font color='red'>No invoices found.</font></td></tr>")
        Else
            Response.Write("<tr bgcolor='#eeeeee'>")
            Response.Write("<td class='formBold'>Invoice ID</td>")
            Response.Write("<td class='formBold'>Partner</td>")
            Response.Write("<td class='formBold'>Country</td>")
            Response.Write("<td class='formBold'></td>")
            Response.Write("</tr>")
            Do Until RecSet.EOF
                invoicePriceListModificationID = RecSet.Fields("invoicePriceListModificationID").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                If rowNumber = 0 Then
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End If
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoicePriceListModificationID & "</td>")
                Response.Write("<td algin='right'>" & partnerName & "</td>")
                Response.Write("<td algin='right'>" & country & "</td>")
                Response.Write("<td algin='right'><a href='activateInvoicePriceListModification.aspx?invoicePriceListModificationID=" & invoicePriceListModificationID & "'>activate</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='4'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then
        invoiceDateYear = Request.Form("invoiceDateYear")
        invoiceDateMonth = Request.Form("invoiceDateMonth")
        invoiceDateDay = Request.Form("invoiceDateDay")
        paymentTerm = Request.Form("paymentTerm")
        invoiceDateYear = CLng(invoiceDateYear)
        invoiceDateMonth = CLng(invoiceDateMonth)
        invoiceDateDay = CLng(invoiceDateDay)
        paymentTerm = CLng(paymentTerm)
        If CLng(invoiceDateMonth) < 10 Then
            invoiceDateMonth = "0" & invoiceDateMonth & ""
        End If
        If CLng(invoiceDateDay) < 10 Then
            invoiceDateDay = "0" & invoiceDateDay & ""
        End If
        invoiceDate = invoiceDateYear & "-" & invoiceDateMonth & "-" & invoiceDateDay
        expiryDate = DateAdd("d", CDbl(paymentTerm), Convert.ToDateTime(invoiceDate))
        dbOpen()
        Response.Write("Select SUM(p1.price) As oldPrice, SUM(p2.price) As newPrice From ((tblInvoicePriceListModificationContent iplmc INNER JOIN tblPriceList p1 ON p1.priceListID = iplmc.oldPriceListID) INNER JOIN tblPriceList p2 ON p2.priceListID = iplmc.newPriceListID) Where iplmc.invoicePriceListModificationID = " & invoicePriceListModificationID & "<br><br>")
        RecSet = Conn.Execute("Select SUM(p1.price) As oldPrice, SUM(p1.uw) As oldUw, SUM(p2.price) As newPrice, SUM(p2.uw) As newUw From ((tblInvoicePriceListModificationContent iplmc INNER JOIN tblPriceList p1 ON p1.priceListID = iplmc.oldPriceListID) INNER JOIN tblPriceList p2 ON p2.priceListID = iplmc.newPriceListID) Where iplmc.invoicePriceListModificationID = " & invoicePriceListModificationID & "")
        If RecSet.EOF Then
            Response.Redirect("activateInvoicePriceListModification.aspx")
        Else
            oldPrice = RecSet.Fields("oldPrice").Value
            oldUw = RecSet.Fields("oldUw").Value
            newPrice = RecSet.Fields("newPrice").Value
            newUw = RecSet.Fields("newUw").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        Response.Write("Select Sum(ap.price) As oldAdditionalPrice From (tblInvoicePriceListModificationContentAdditionalOld iplmcao INNER JOIn tblAdditionalProduct ap ON ap.additionalProductID = iplmcan.additionalProductID) Where invoicePriceListModificationID = " & invoicePriceListModificationID & "<br>")
        RecSet = Conn.Execute("Select Sum(ap.price) As oldAdditionalPrice From (tblInvoicePriceListModificationContentAdditionalOld iplmcao INNER JOIn tblAdditionalProduct ap ON ap.additionalProductID = iplmcao.additionalProductID) Where iplmcao.invoicePriceListModificationID = " & invoicePriceListModificationID & "")
        If Not RecSet.EOF Then
            oldAdditionalPrice = RecSet.Fields("oldAdditionalPrice").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        Response.Write("Select Sum(ap.price) As newAdditionalPrice From (tblInvoicePriceListModificationContentAdditionalNew iplmcan INNER JOIn tblAdditionalProduct ap ON ap.additionalProductID = iplmcan.additionalProductID) Where iplmcan.invoicePriceListModificationID = " & invoicePriceListModificationID & "<br>")
        RecSet = Conn.Execute("Select Sum(ap.price) As newAdditionalPrice From (tblInvoicePriceListModificationContentAdditionalNew iplmcan INNER JOIn tblAdditionalProduct ap ON ap.additionalProductID = iplmcan.additionalProductID) Where iplmcan.invoicePriceListModificationID = " & invoicePriceListModificationID & "")
        If Not RecSet.EOF Then
            newAdditionalPrice = RecSet.Fields("newAdditionalPrice").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(oldAdditionalPrice) = True Then
            oldAdditionalPrice = 0
        End If
        If IsDBNull(newAdditionalPrice) = True Then
            newAdditionalPrice = 0
        End If
        oldPrice = oldPrice + oldAdditionalPrice
        newPrice = newPrice + newAdditionalPrice
        invoiceAmount = CDbl(newPrice) - CDbl(oldPrice)
        invoiceAmount = Replace(CStr(invoiceAmount), ",", ".")
        invoiceClaim = invoiceAmount
        uw = CDbl(newUw) - CDbl(oldUw)
        uw = Replace(CStr(uw), ",", ".")
        userID = fragusLeasingUserID
        Response.Write("userID = " & userID & "<br>")
        Response.Write("oldPrice = " & oldPrice & " (" & oldAdditionalPrice & ")<br>")
        Response.Write("newPrice = " & newPrice & " (" & newAdditionalPrice & ")<br>")
        Response.Write("invoiceAmount = " & invoiceAmount & "<br>")
        Response.Write("invoiceClaim = " & invoiceClaim & "<br>")
        Response.Write("oldUw = " & oldUw & "<br>")
        Response.Write("newUw = " & newUw & "<br>")
        Response.Write("UW = " & uw & "<br>")
        Response.Write("invoiceDate = " & invoiceDate & "<br>")
        Response.Write("expiryDate = " & expiryDate & "<br>")
        Response.Write("paymentTerm = " & paymentTerm & "<br>")
        dbOpen()
        Conn.Execute("Update tblInvoicePriceListModification Set userID = " & userID & ", invoiceAmount = " & invoiceAmount & ", invoiceClaim = " & invoiceClaim & ", uw = " & uw & ", invoiceDate = '" & invoiceDate & "', expiryDate = '" & expiryDate & "', paymentTerm = " & paymentTerm & " Where invoicePriceListModificationID = " & invoicePriceListModificationID & "")
        dbClose()
        Response.Redirect("activateInvoicePriceListModification.aspx?subDo=%20-%20ACTIVATED")
    End If
%>
